package com.venmo.view;

import ohos.agp.components.Component;
import ohos.agp.utils.RectFloat;

public final class ArrowAlignmentHelper {

    public static float calculateArrowMidPoint(TooltipView view, RectFloat rect) {
        int offset = view.getAlignmentOffset();
        float middle = 0f;

        switch (view.getArrowAlignment()) {
            case START:
                middle = offset == 0 ? rect.getWidth() / 4 : offset;
                break;
            case CENTER:
                middle = rect.getWidth() / 2;
                if (offset > 0) {
                    throw new IllegalArgumentException(
                            "Offsets are not support when the tooltip " +
                                    "arrow is anchored in the middle of the view.");
                }
                break;
            case END:
                middle = rect.getWidth();
                middle -= (offset == 0 ? rect.getWidth() / 4 : offset);
                break;
            case ANCHORED_VIEW:
                middle = rect.getWidth() / 2;
                if (view.getAnchoredViewId() != Component.ID_DEFAULT) {
                    Component anchoredView = ((Component) view.getComponentParent())
                            .findComponentById(view.getAnchoredViewId());
                    middle += anchoredView.getContentPositionX()
                            + (float) (anchoredView.getWidth()) / 2 - view.getContentPositionX()
                            - (float) (view.getWidth()) / 2;
                }
                break;
        }
        return middle;
    }
}
